package gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import model.SentimentModel;
import model.process.MainProcess;

import process.Run;

import Comment.Comment;
import Comment.Result;
import Comment.Comment.*;

public class Gui extends JFrame{
	private JLabel commentLabel;
	private JLabel resultLabel;
	private JPanel panel;
	private JPanel mainPanel;
	private JPanel mainPanel2;
	private JPanel mainPanel3;
	private JRadioButton radioMenuForText;
	private JRadioButton radioMenuForFile;
	private ButtonGroup bg;
	private JPanel northOfSouthPanel;
	private JPanel centreOfSouthPanel;
	private JButton runButton = new JButton("RUN");
	private JButton refreshButton = new JButton("Refresh");
	private JButton start = new JButton("START");
	private JButton home = new JButton("HOME");
	private JPanel panelForHoemForFile = new JPanel();
	private JButton homeForFile = new JButton("HOME");
	private JScrollPane commentScrollPane;
	private JScrollPane resultScrollPane;
	private JTextArea commentTextArea;
	private JTextArea resultTextArea;
	private int frameSize;
	private int frameHigh;
	private JPanel northPanel;
	private JPanel southPanel;
	private JLabel runTimeLabel;
	private String time;
	private String result;
	//mainPanel 3
	private JPanel ceterPanelOfMainPanel3;
	private JButton chooseFile = new JButton("FileChooser");
	private String nameFileChoice = "/data";
	private JTextField inputTextFiels = new JTextField(20);
	private JTextField outputTextField = new JTextField(20);
	private JLabel inputLabel = new JLabel("InputFile");
	private JLabel outputLabel = new JLabel("OutputFile");
	
	private JPanel southPanelOfMainPanel3;
	//private JPanel panelForProcessFileButton = new JPanel();
	private JButton processFileButton = new JButton("RUN");
	private String runningText = "<html><font color=green size=15>" + "<b>" + 
"Running....." + "</b>" + "</font>" + "</html>";
	private JLabel processLabel = new JLabel(runningText);
	private String positiveOrNegativeResultPath = Run.positiveOrNegativeResultForOneEntityCommentsPath;
	private JPanel panelForSouthOfMainPanel3;
	private JLabel timeLabel;
	private JLabel precisionLabel;
	private JLabel recallLabel;
	
	private JButton backForMain3 = new JButton("Back");
	public Gui(){
		processFileButton.setBackground(Color.green);
		processLabel.setText(runningText);
		panel = new JPanel();
		this.add(panel);
		result = "Nothing";
		time = "runTime: 0.00s";
		mainPanel = new JPanel();
		mainPanel.setVisible(false);
		panel.add(mainPanel);
		mainPanel2 = new JPanel();
		mainPanel3 = new JPanel();
		mainPanel3.setVisible(false);
		panel.add(mainPanel3);
		panel.add(mainPanel2);
		bg = new ButtonGroup();
		radioMenuForText = new JRadioButton("Comment");
		radioMenuForFile = new JRadioButton("File");
		bg.add(radioMenuForFile);
		bg.add(radioMenuForText);
		radioMenuForText.setSelected(true);
		northPanel = new JPanel();
		southPanel = new JPanel();
		northOfSouthPanel = new JPanel();
		centreOfSouthPanel = new JPanel();
		commentTextArea = new JTextArea(7,60);
		resultTextArea = new JTextArea(result,10,60);
		resultTextArea.setEditable(false);
		commentScrollPane = new JScrollPane(commentTextArea);
		commentLabel = new JLabel("Comment");
		
		resultLabel = new JLabel("     Result");
		runTimeLabel = new JLabel(time);
		resultScrollPane = new JScrollPane(resultTextArea);
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(northPanel, BorderLayout.NORTH);
		northPanel.add(commentLabel);
		northPanel.add(commentScrollPane);
		mainPanel.add(runButton, BorderLayout.CENTER);
		mainPanel.add(southPanel, BorderLayout.SOUTH);
		southPanel.setLayout(new BorderLayout());
		southPanel.add(northOfSouthPanel, BorderLayout.NORTH);
		southPanel.add(centreOfSouthPanel, BorderLayout.CENTER);
		northOfSouthPanel.add(resultLabel);
		northOfSouthPanel.add(resultScrollPane);
		centreOfSouthPanel.add(refreshButton);
		centreOfSouthPanel.add(home);
		centreOfSouthPanel.add(runTimeLabel);
		
		southPanel.setVisible(false);
		
		mainPanel2.add(radioMenuForText);
		mainPanel2.add(radioMenuForFile);
		mainPanel2.add(start);
		//mainPanel3
		ceterPanelOfMainPanel3 = new JPanel();
		mainPanel3.setLayout(new BorderLayout());
		panelForHoemForFile.add(homeForFile);
		mainPanel3.add(panelForHoemForFile, BorderLayout.NORTH);
		ceterPanelOfMainPanel3.add(chooseFile);
		ceterPanelOfMainPanel3.add(inputLabel);
		ceterPanelOfMainPanel3.add(inputTextFiels);
		ceterPanelOfMainPanel3.add(outputLabel);
		ceterPanelOfMainPanel3.add(outputTextField);
		mainPanel3.add(ceterPanelOfMainPanel3, BorderLayout.CENTER);
		inputTextFiels.setText("E:/research/projectCoding/VietnameseSentimentOfComment/input/oneEntityComments.txt");
		outputTextField.setText("E:/research/projectCoding/VietnameseSentimentOfComment/result/resultForOneEntityComments.txt");
		
		southPanelOfMainPanel3 = new JPanel();
		mainPanel3.add(southPanelOfMainPanel3, BorderLayout.SOUTH);
		//panelForProcessFileButton.add(processFileButton);
		southPanelOfMainPanel3.setLayout(new BorderLayout());
		southPanelOfMainPanel3.add(processFileButton, BorderLayout.NORTH);
		southPanelOfMainPanel3.add(processLabel,BorderLayout.CENTER);
		processLabel.setVisible(false);
		panelForSouthOfMainPanel3 = new JPanel();
		timeLabel = new JLabel();
		precisionLabel = new JLabel("100%");
		recallLabel = new JLabel("100%");
		southPanelOfMainPanel3.add(panelForSouthOfMainPanel3, BorderLayout.SOUTH);
		panelForSouthOfMainPanel3.add(timeLabel);
//		timeLabel.setVisible(false);
		panelForSouthOfMainPanel3.add(precisionLabel);
		panelForSouthOfMainPanel3.add(recallLabel);
		panelForSouthOfMainPanel3.add(backForMain3);
//		backForMain3.setVisible(false);
//		precisionLabel.setVisible(false);
//		recallLabel.setVisible(false);
		panelForSouthOfMainPanel3.setVisible(false);
		buttonWorking();
		setTitle("VietNamese Sentiment");
		setResizable(false);
		setSize((setFrameSize(400)),(setFrameHigh(100)));
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(400, 200);
	}
	
	private void buttonWorking() {
		// TODO Auto-generated method stub
		runWorking();
		refreshWorking();
		startWorking();
		homeWorking();
		homeForFileWorking();
		processForFileWorking();
		chooseFileWorking();
		backForMainWorking();
	}
	
	private void backForMainWorking() {
		// TODO Auto-generated method stub
		backForMain3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
//				southPanelOfMainPanel3.setVisible(false);
//				chooseFile.setVisible(true);
//				homeForFile.setVisible(true);
				inputTextFiels.setEditable(true);
				outputTextField.setEditable(true);
				processLabel.setVisible(false);
				processFileButton.setEnabled(true);
				homeForFile.setEnabled(true);
//				processFileButton.setVisible(true);
				setSize(800, 150);
//				backForMain3.setVisible(false);
//				timeLabel.setVisible(false);
				panelForSouthOfMainPanel3.setVisible(false);
			}
		});
	}

	private void chooseFileWorking() {
		// TODO Auto-generated method stub
		chooseFile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				JFileChooser fchooser = new JFileChooser("E:/research/projectCoding/VietnameseSentimentOfComment/input");
				positiveOrNegativeResultPath = Run.positiveOrNegativeResultForOneEntityCommentsPath;
				inputTextFiels.setText(nameFileChoice);
				//outputTextField.setText(Run.resultForOneEntityCommentsPath);
				if(fchooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
					nameFileChoice = fchooser.getSelectedFile().getPath();
					inputTextFiels.setText(nameFileChoice);
					positiveOrNegativeResultPath = Run.positiveOrNegativeResultForOneEntityCommentsPath;
					if(nameFileChoice.compareTo("E:\\research\\projectCoding\\VietnameseSentimentOfComment\\input\\oneEntityComments.txt") == 0){
						outputTextField.setText(Run.resultForOneEntityCommentsPath);
						positiveOrNegativeResultPath = Run.positiveOrNegativeResultForOneEntityCommentsPath;
					}
					else if(nameFileChoice.compareTo("E:\\research\\projectCoding\\VietnameseSentimentOfComment\\input\\multEntityComments.txt") == 0){
						outputTextField.setText(Run.resultForMultEntityCommentsPath);
						positiveOrNegativeResultPath = Run.positiveOrNegativeResultForMultEntityCommentsPath;
					}
					else if(nameFileChoice.compareTo("E:\\research\\projectCoding\\VietnameseSentimentOfComment\\input\\comments.txt") == 0){
						outputTextField.setText(Run.resultForCommentsPath);
						positiveOrNegativeResultPath = Run.PositiveOrNegativeResultForCommentsPath;
					}
					
				}
				inputTextFiels.setText(nameFileChoice);
			}
		});
	}

	

	private void processForFileWorking() {
		// TODO Auto-generated method stub
		processFileButton.addActionListener(new ActionListener() {		
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String input = inputTextFiels.getText();
				String output = outputTextField.getText();
//				processLabel.setVisible(true);
//				processFileButton.setVisible(false);
//				chooseFile.setEnabled(false);
//				inputTextFiels.setEditable(false);
//				outputTextField.setEditable(false);
//				homeForFile.setVisible(false);
				processLabel.setVisible(true);
				if(input != null && output!=null){
					if(input.compareTo("")!=0&&output.compareTo("")!=0){
						long time = 0;
						homeForFile.setEnabled(false);
						time = Run.processForCommentsFile(input, output, positiveOrNegativeResultPath);
						setSize(800, 220);
						processFileButton.setEnabled(false);
						
						if(time>0){
							
							String str1 = "<html><font color=blue size=10>" + "<b>" + 
							"Finish" + "</b>" + "</font>" + "</html>";
							processLabel.setText(str1);
							String str2 = "<html><font color=#9932CC size=3>" + "<b>" + 
							"Time: " +Long.toString(time)+ "</b>" + "</font>" + "</html>";
							timeLabel.setText(str2);
//							timeLabel.setVisible(true);
//							backForMain3.setVisible(true);
							MainProcess mainProcess = new MainProcess();
							String p = "";
							String r = "";
							p = mainProcess.calPrecision(
									Run.realPos, Run.realNeg, Run.realPN, Run.sysPos, Run.sysNeg, Run.sysPN);
							r = mainProcess.calRecall(
									Run.realPos, Run.realNeg, Run.realPN, Run.sysPos, Run.sysNeg, Run.sysPN);
							precisionLabel.setText("<html><font color=red size=3>"+"	P="+p+"	"+"</html>");
							recallLabel.setText("<html><font color=red size=3>"+"	R="+r+"	"+"</html>");
							panelForSouthOfMainPanel3.setVisible(true);
							System.out.println(time);
						}

					}
				}
			}
		});
	}

	private void homeForFileWorking() {
		// TODO Auto-generated method stub
		this.homeForFile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				setSize(400, 100);
				mainPanel3.setSize(800, 150);
				processFileButton.setVisible(true);
				processLabel.setVisible(false);
				mainPanel3.setVisible(false);
				mainPanel2.setVisible(true);
			}
		});
	}

	private void homeWorking() {
		// TODO Auto-generated method stub
		this.home.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				setSize(400, 100);
				resultTextArea.setText("");
				southPanel.setVisible(false);
				mainPanel.setVisible(false);
				mainPanel2.setVisible(true);
			}
		});
	}

	private void startWorking() {
		// TODO Auto-generated method stub
		this.start.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(radioMenuForText.isSelected()){
					runButton.setVisible(true);
					southPanel.setVisible(false);
					commentTextArea.setEditable(true);
					setSize(800, 200);
					mainPanel.setVisible(true);
					mainPanel2.setVisible(false);
				}
				else{
					setSize(800, 150);
					mainPanel2.setVisible(false);
					mainPanel3.setVisible(true);
					homeForFile.setVisible(true);
				}
			}
		});
	}

	//for comment panel
	private void runWorking() {
		// TODO Auto-generated method stub
		this.runButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				southPanel.setVisible(!southPanel.isVisible());
				setSize(800, 400);
				runButton.setVisible(false);
				commentTextArea.setEditable(false);
				String com = commentTextArea.getText();
				Comment comment = new Comment(com);
				Result result = comment.getResult();
				if(result.getOneOrMultiEntity() == 1){
					resultTextArea.setText(Float.toString(result.getSentimentForOneCase()));
				}
				else if(result.getOneOrMultiEntity() == 2){
					ArrayList<SentimentModel> resultForMultiEntity = result.getSentimentForMultCase();
					for(int i=0; i<resultForMultiEntity.size(); i++){
						resultTextArea.setText(resultForMultiEntity.get(i).getWordSentiment()+" "
												+Float.toString(resultForMultiEntity.get(i).getScore())+"\n");
					}
				}
			}
		}
		);
	}

	public void refreshWorking() {
		// TODO Auto-generated method stub
		this.refreshButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				commentTextArea.setText("");
				result = "";
				resultTextArea.setText(result);
				setSize(800, 200);
				runButton.setVisible(true);
				southPanel.setVisible(!southPanel.isVisible());
				commentTextArea.setEditable(true);
			}
		});
	}


	public int setFrameSize(int frameSize) {
		this.frameSize = frameSize;
		return frameSize;
	}
	public int getFrameSize() {
		return frameSize;
	}
	public int setFrameHigh(int frameHigh) {
		this.frameHigh = frameHigh;
		return frameHigh;
	}
	public int getFrameHigh() {
		return frameHigh;
	}
}